<?php
/**
 * Article controller.
 *  
 * @author Nguyen Hai Ha
 * @version 1.0
 */

class Administrator_ArticleController extends Zend_Controller_Action {
	protected $_baseUrl;
	protected $_contextPath;
	protected $_utilities;
	protected $_errors;
	protected $_auth;
	
	public function init() {
		$this->_baseUrl = $this->_request->getBaseURL ();
		$this->_contextPath = "administrator/article";
		
		$this->view->assign ( "baseUrl", $this->_baseUrl );
		$this->view->assign ( "contextPath", $this->_contextPath );
		
		$this->_utilities = new Utilities ( );
		$this->view->assign ( "utilities", $this->_utilities );
		
		$this->_auth = Zend_Auth::getInstance()->getIdentity();
		
		$this->_errors = array ();
	}
	
	public function indexAction() {
		$this->_redirect ( $this->_contextPath . "/view-all-article" );
	}
	
	public function searchAction() {
		if ($this->_request->isPost()){

		}
		
		$this->render("search");
	}
	
	public function articleErrorAction() {
		$this->render ( "article-error" );
	}
	
	public function viewAllArticleAction() {
		$articleDAO = new ArticleModel ( );
		$published = ( int ) 1;
		
		//Begin: render script for paging.
		$paging = new Paging ( );
		//Get item on page.
		//Get total page.
		//Get current page.
		$itemPerPage = Constant::ARTICLE_LIST_ITEM;
		$totalRecords = $articleDAO->getTotalRecords ( $published );
		$currentPages = ( int ) $this->_request->getParam ( 'page' );
		if ($currentPages == 0)
			$currentPages = 1;
			//Set url for paging.	
		//Render paging for page.	
		//Render paging title for page.	
		$url = $this->_baseUrl . "/" . $this->_contextPath . "/view-all-article";
		$page = $paging->doPaging ( $totalRecords, $url, $currentPages, $itemPerPage );
		$page_title = $paging->doPageSeparator ( $totalRecords, $currentPages, $itemPerPage );
		//Set for view.
		$this->view->assign ( "page", $page );
		$this->view->assign ( "page_title", $page_title );
		$this->view->assign ( "currentPage", $currentPages );
		//End: render script for paging.		
		

		$listArticles = $articleDAO->getAllArticle ( $published, $currentPages, $itemPerPage );
		
		$this->view->assign ( "listArticles", $listArticles );
		$this->render ( "view-all-article" );
	}
	
	public function unpublishedArticleAction() {
		$articleDAO = new ArticleModel ( );
		
		$request = $this->getRequest ();
		$article_id = ( int ) $request->getParam ( 'article_id' );
		$currentPage = ( int ) $request->getParam ( 'currentPage' );
		
		$articleDAO->updateArticle ( array ('art_published' => 0 ), $article_id );
		
		$this->_redirect ( $this->_contextPath . "/view-all-article/page/" . $currentPage );
		return;
	}
	
	public function addArticleAction() {
		if ($this->_request->isPost ()) {
			
			$article_title = ( string ) stripslashes ( $this->_request->getPost ( "article_title" ) );
			$article_lead = ( string ) stripslashes ( $this->_request->getPost ( "article_lead" ) );
			$article_content = ( string ) stripslashes ( $this->_request->getPost ( "article_content" ) );
			$published = ( int ) $this->_request->getPost ( "published" ) ? "1" : "0";

			if ($published)
				$art_published_by = $this->_auth->usr_mobile;
			else
			 	$art_published_by = "";

			// Validate.
			if (! strlen ( $article_title ))
				$this->_errors ['title_null'] = "Ban phai nhap title cua tin";
			if (! strlen ( $article_content ))
				$this->_errors ['content_null'] = "Ban phai nhap noi dung cua tin.";
			
			if (count ( $this->_errors )){
				$image_name = "";
			}
			else {
				// Do upload image.
				$folder = $_SERVER ['DOCUMENT_ROOT'] . '/' . $this->_baseUrl . "/public/images/article/";
				$images = $_FILES ['image_file'];
				$image_name = $this->_utilities->uploadImage ( $images, $folder );
			}
			if ($image_name)
				$art_has_image = 1;
			else
				$art_has_image = 0;
				
			$article = array ('art_title' => $article_title, 'art_has_image' => $art_has_image, 'art_image' => $image_name, 'art_lead' => $article_lead, 'art_content' => $article_content, 'art_published' => $published, 'art_published_by' => $art_published_by );
			if (count ( $this->_errors )) {
				$this->view->assign ( "errors", $this->_errors );
				$this->view->assign ( "article", $article );
				$this->render ( "add-article" );
				
				return;
			}
			
			// Do insert.
			$articleDAO = new ArticleModel ( );
			$articleDAO->insertArticle ( $article );
			
			if ($published) {
				$this->_redirect ( $this->_contextPath . "/view-all-article" );
			} else {
				$this->_redirect ( $this->_contextPath . "/view-all-article-unpublished" );
			}
			
			return;
		}
		// Neu khong phai su kien submit, render man hinh nhap du lieu.
		$this->render ( "add-article" );
	}
	
	public function editArticleAction() {
		$request = $this->getRequest ();
		$article_id = ( int ) $request->getParam ( 'article_id' );
		$currentPage = ( int ) $request->getParam ( 'currentPage' );
		if ($article_id) {
			$articleDAO = new ArticleModel ( );
			$article = $articleDAO->getArticleById ( $article_id );
			if ($article) {
				$this->view->assign ( "article", $article );
				$this->view->assign ( "currentPage", $currentPage );
				$this->render ( "edit-article" );
				return;
			}
		}
		
		$this->_redirect ( $this->_contextPath . "/article-error" );
	}
	
	public function saveEditArticleAction() {
		if ($this->_request->isPost ()) {
			$f = new Zend_Filter_StripTags ( );
			
			$request = $this->getRequest ();
			$article_id = ( int ) $request->getPost ( 'article_id' );
			$currentPage = ( int ) $request->getPost ( 'currentPage' );
			
			$article_title = ( string ) stripslashes ( $f->filter ( $this->_request->getPost ( "article_title" ) ) );
			$article_lead = ( string ) stripslashes ( $f->filter ( $this->_request->getPost ( "article_lead" ) ) );
			$article_content = ( string ) stripslashes ( $this->_request->getPost ( "article_content" ) );
			$published = ( int ) $this->_request->getPost ( "published" ) ? "1" : "0";
			// old image.
			$article_image = ( string ) ($this->_request->getPost ( "art_image" ));
			$article_has_image = ( int ) $this->_request->getPost ( "art_has_image" );
				
			if ($published)
				$art_published_by = $this->_auth->usr_mobile;
			else
			 	$art_published_by = "";
			
			// Validate.
			if (! strlen ( $article_title ))
				$this->_errors ['title_null'] = "Ban phai nhap title cua tin";
			if (! strlen ( $article_content ))
				$this->_errors ['content_null'] = "Ban phai nhap noi dung cua tin.";
				
			if (!count ( $this->_errors )){
				// Do upload image.
				$folder = $_SERVER ['DOCUMENT_ROOT'] . '/' . $this->_baseUrl . "/public/images/article/";
				$images = $_FILES ['image_file'];
				$image_name = $this->_utilities->uploadImage ( $images, $folder, $article_image );
			}
			
			if ($image_name == $article_image){
				$art_has_image = $article_has_image;
			} else {
				if ($image_name)
					$art_has_image=1;
				else
					$art_has_image=0;
			}

			$article = array (
				'art_id' => $article_id, 
				'art_title' => $article_title, 
				'art_has_image' => $art_has_image, 
				'art_image' => $image_name, 
				'art_lead' => $article_lead, 
				'art_content' => $article_content, 
				'art_published' => $published, 
				'art_published_by' => $art_published_by 
			);
			
			if (count ( $this->_errors )) {
				$this->view->assign ( "errors", $this->_errors );
				$this->view->assign ( "article", $article );
				$this->view->assign ( "currentPage", $currentPage );
				$this->render ( "edit-article" );
				
				return;
			}
			
			// Do update.
			$articleDAO = new ArticleModel ( );
			$articleDAO->updateArticle ( $article, $article_id );
			
			$this->_redirect ( $this->_contextPath . "/view-all-article/page/" . $currentPage );
			return;
		}
		
		$this->_redirect ( $this->_contextPath . "/article-error" );
	}
	
	public function deleteArticleAction() {
		$request = $this->getRequest ();
		$article_id = ( int ) $request->getParam ( 'article_id' );
		$currentPage = ( int ) $request->getParam ( 'currentPage' );
		if ($article_id) {
			$articleDAO = new ArticleModel ( );
			$article = $articleDAO->getArticleById ( $article_id );
			
			// Kiem tra xem article co ton tai hay khong.
			if ($article) {
				$articleDAO->deleteArticle ( $article_id );
				
				if ($article->art_published) {
					$this->_redirect ( $this->_contextPath . "/view-all-article/page/" . $currentPage );
				} else {
					$this->_redirect ( $this->_contextPath . "/view-all-article-unpublished/page/" . $currentPage );
				}
				
				return;
			}
		}
		
		$this->_redirect ( $this->_contextPath . "/article-error" );
	}
	
	public function viewArticleAction() {
		$request = $this->getRequest ();
		$article_id = ( int ) $request->getParam ( 'article_id' );
		if ($article_id) {
			$articleDAO = new ArticleModel ( );
			$article = $articleDAO->getArticleById ( $article_id );
			
			// Kiem tra xem article co ton tai hay khong.
			if ($article) {
				$this->view->assign ( "article", $article );
				
				$this->render ( "view-article" );
				return;
			}
		}
		
		$this->_redirect ( $this->_contextPath . "/article-error" );
	}
	
	/** Phan unpublic article*/
	
	public function viewAllArticleUnpublishedAction() {
		$articleDAO = new ArticleModel ( );
		$published = ( int ) 0;
		
		//Begin: render script for paging.
		$paging = new Paging ( );
		//Get item on page.
		//Get total page.
		//Get current page.
		$itemPerPage = Constant::ARTICLE_LIST_ITEM;
		$totalRecords = $articleDAO->getTotalRecords ( $published );
		$currentPages = ( int ) $this->_request->getParam ( 'page' );
		if ($currentPages == 0)
			$currentPages = 1;
			//Set url for paging.	
		//Render paging for page.	
		//Render paging title for page.	
		$url = $this->_baseUrl . "/" . $this->_contextPath . "/view-all-article-unpublished";
		$page = $paging->doPaging ( $totalRecords, $url, $currentPages, $itemPerPage );
		$page_title = $paging->doPageSeparator ( $totalRecords, $currentPages, $itemPerPage );
		//Set for view.
		$this->view->assign ( "page", $page );
		$this->view->assign ( "page_title", $page_title );
		$this->view->assign ( "currentPage", $currentPages );
		//End: render script for paging.
		

		$listArticles = $articleDAO->getAllArticle ( $published, $currentPages, $itemPerPage );
		
		$this->view->assign ( "listArticles", $listArticles );
		$this->render ( "view-all-article-unpublished" );
	}
	
	public function publishedArticleAction() {
		$articleDAO = new ArticleModel ( );
		
		$published_by = $this->_auth->usr_mobile;
		
		$request = $this->getRequest ();
		$article_id = ( int ) $request->getParam ( 'article_id' );
		$currentPage = ( int ) $request->getParam ( 'currentPage' );
		
		$articleDAO->updateArticle ( array ('art_published' => 1, 'art_published_by'=>$published_by ), $article_id );
		
		$this->_redirect ( $this->_contextPath . "/view-all-article-unpublished/page/" . $currentPage );
		return;
	}
	
	public function editArticleUnpublishedAction() {
		$request = $this->getRequest ();
		$article_id = ( int ) $request->getParam ( 'article_id' );
		$currentPage = ( int ) $request->getParam ( 'currentPage' );
		if ($article_id) {
			$articleDAO = new ArticleModel ( );
			$article = $articleDAO->getArticleById ( $article_id );
			if ($article) {
				$this->view->assign ( "article", $article );
				$this->view->assign ( "currentPage", $currentPage );
				$this->render ( "edit-article-unpublished" );
				return;
			}
		}
		
		$this->_redirect ( $this->_contextPath . "/article-error" );
	}
	
	public function saveEditArticleUnpublishedAction() {
		if ($this->_request->isPost ()) {
			$f = new Zend_Filter_StripTags ( );
			
			$request = $this->getRequest ();
			$article_id = ( int ) $request->getPost ( 'article_id' );
			$currentPage = ( int ) $request->getPost ( 'currentPage' );
			
			$article_title = ( string ) stripslashes ( $f->filter ( $this->_request->getPost ( "article_title" ) ) );
			$article_lead = ( string ) stripslashes ( $f->filter ( $this->_request->getPost ( "article_lead" ) ) );
			$article_content = ( string ) stripslashes ( $this->_request->getPost ( "article_content" ) );
			$published = ( int ) $this->_request->getPost ( "published" ) ? "1" : "0";
			// old image.
			$article_image = ( string ) ($this->_request->getPost ( "art_image" ));
			$article_has_image = ( int ) $this->_request->getPost ( "art_has_image" );

			// Validate.
			if (! strlen ( $article_title ))
				$this->_errors ['title_null'] = "Ban phai nhap title cua tin";
			if (! strlen ( $article_content ))
				$this->_errors ['content_null'] = "Ban phai nhap noi dung cua tin.";
				
			if (!count ( $this->_errors )){
				// Do upload image.
				$folder = $_SERVER ['DOCUMENT_ROOT'] . '/' . $this->_baseUrl . "/public/images/article/";
				$images = $_FILES ['image_file'];
				$image_name = $this->_utilities->uploadImage ( $images, $folder, $article_image );
			}
			
			if ($image_name == $article_image){
				$art_has_image = $article_has_image;
			} else {
				if ($image_name)
					$art_has_image=1;
				else
					$art_has_image=0;
			}
			
			$article = array ('art_id' => $article_id, 'art_title' => $article_title, 'art_has_image' => $art_has_image, 'art_image' => $image_name, 'art_lead' => $article_lead, 'art_content' => $article_content, 'art_published' => $published );
			
			// Validate.
			if (! strlen ( $article_title ))
				$this->_errors ['title_null'] = "Ban phai nhap title cua tin";
			if (! strlen ( $article_content ))
				$this->_errors ['content_null'] = "Ban phai nhap noi dung cua tin.";
			if (count ( $this->_errors )) {
				$this->view->assign ( "errors", $this->_errors );
				$this->view->assign ( "article", $article );
				$this->view->assign ( "currentPage", $currentPage );
				$this->render ( "edit-article-unpublished" );
				
				return;
			}
			
			// Do update.
			$articleDAO = new ArticleModel ( );
			$articleDAO->updateArticle ( $article, $article_id );
			
			$this->_redirect ( $this->_contextPath . "/view-all-article-unpublished/page/" . $currentPage );
			return;
		}
		
		$this->_redirect ( $this->_contextPath . "/article-error" );
	}

}//End class.